package com.dgshanger.sanhxiaofeisc.ui.home.activity

import android.Manifest
import android.content.Intent
import android.os.Environment
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.blankj.utilcode.util.PermissionUtils
import com.dgshanger.sanhxiaofeisc.BuildConfig
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.mvp.contract.ShopEnterContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.ShopEnterPresenter
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import com.dgshanger.sanhxiaofeisc.ui.home.utils.TextDialog
import com.xx.baseuilibrary.mvp.BaseMvpActivity
import com.xx.baseutilslibrary.common.ImageChooseHelper
import com.dgshanger.sanhxiaofeisc.dialog.ChooseImageDialogWrapper
import kotlinx.android.synthetic.main.activity_shop_enter.*
import java.io.File
import java.io.FileInputStream


/**
 * Created by JacobHHH on 2018/4/22.
 * 商家入驻
 */
class ShopEnterActivity : BaseMvpActivity<ShopEnterContract.Model,ShopEnterContract.View,ShopEnterPresenter>(),ShopEnterContract.View, View.OnClickListener {


    override fun createPresenter(): ShopEnterPresenter = ShopEnterPresenter()

    override fun getActivityLayoutId(): Int = R.layout.activity_shop_enter
    private lateinit var imageChooseHelper: ImageChooseHelper
    private var fileList : HashMap<String,File> ?= HashMap()
    override fun initData() {
        initView()
        imageChooseHelper = ImageChooseHelper.Builder()
                .setUpActivity(this)
                .setAuthority("${BuildConfig.APPLICATION_ID}.fileprovider")//设置文件提供者
                .setDirPath(Environment.getExternalStorageDirectory().absolutePath + "/" + BuildConfig.APPLICATION_ID)
                .isCrop(false)//开启裁剪
                .setCompressQuality(100)//压缩质量[1,100]
                .setOnFinishChooseImageListener { uri, file ->

                    //显示选好得图片
                    when(imageType){
                        0-> {
                            ImageLoader.loadFileAndSave(mContext, iv_business, file)
                            fileList!!["file0"]=file
                        }
                        1->{
                            ImageLoader.loadFileAndSave(mContext,iv_front,file)
                            fileList!!["file1"]=file
                        }
                        2->{
                            ImageLoader.loadFileAndSave(mContext,iv_contrary,file)
                            fileList!!["file2"]=file
                        }
                        3->{
                            ImageLoader.loadFileAndSave(mContext,iv_special,file)
                            fileList!!["file3"]=file
                        }
                    }
                }
                .create()

    }

    @Throws(Exception::class)
    fun getFileSize(file: File?): Long {
        if (file == null) {
            return 0
        }
        var size: Long = 0
        if (file!!.exists()) {
            var fis: FileInputStream? = null
            fis = FileInputStream(file)
            size = fis!!.available().toLong()
        }
        return size
    }

    private fun initView() {
        iv_business.setOnClickListener(this)
        iv_front.setOnClickListener(this)
        iv_contrary.setOnClickListener(this)
        iv_special.setOnClickListener(this)
        btn_submit.setOnClickListener(this)

    }

    override fun initEvent() {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        imageChooseHelper.onActivityResult(requestCode, resultCode, data)
    }


    private var imageType : Int = -1
    private var map : HashMap<String,String>?=HashMap()
    override fun onClick(view: View?) {
        PermissionUtils.permission(Manifest.permission.CAMERA, Manifest.permission_group.STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .callback(object : PermissionUtils.SimpleCallback{
                    override fun onGranted() {
                        when(view!!.id){
                            R.id.iv_business->{//营业执照
                                imageType=0
                                //被给予权限,调起选图弹窗
                                ChooseImageDialogWrapper(imageChooseHelper)
                                        .showChooseImage()
                            }
                            R.id.iv_front->{//正面
                                imageType=1
                                //被给予权限,调起选图弹窗
                                ChooseImageDialogWrapper(imageChooseHelper)
                                        .showChooseImage()
                            }
                            R.id.iv_contrary->{//反面
                                imageType=2
                                //被给予权限,调起选图弹窗
                                ChooseImageDialogWrapper(imageChooseHelper)
                                        .showChooseImage()
                            }
                            R.id.iv_special->{//特殊附件
                                imageType=3
                                //被给予权限,调起选图弹窗
                                ChooseImageDialogWrapper(imageChooseHelper)
                                        .showChooseImage()
                            }
                            R.id.btn_submit->{
                                map!!.put("shop_company_name",et_unit.text.trim().toString())//单位全称
                                map!!.put("shop_company_address",et_unit_address.text.trim().toString())//单位地址
                                map!!.put("shop_company_email",et_email.text.trim().toString())//邮箱
                                map!!.put("shop_company_contract",et_phone.text.trim().toString())//联系电话
                                map!!.put("shop_company_corporate",et_name.text.trim().toString())//法人名字
                                map!!.put("shop_company_corporate_id",et_card_num.text.trim().toString())//法人身份证号码
                                getPresenter().checkParameter(fileList,map)

                            }
                        }
                    }

                    override fun onDenied() {
                        //被拒绝
                        showToast("拒绝给予权限会导致该功能不能正常使用")
                    }

                })
                .rationale{shouldRequest -> shouldRequest.again(true)}
                .request()
    }

    override fun checkSuccess() {
        val textDialog = TextDialog(mContext)
        textDialog.setOnBtnClickListener(object : TextDialog.OnBtnClickListener{
            override fun confirm() {
                getPresenter().submitAudit(fileList,map)
                textDialog.dismiss()
            }
            override fun cancel() {
                textDialog.dismiss()
            }
        })
        textDialog.show()
    }

    override fun submitAuditSuccess(code : String?) {
        val intent = Intent(mContext, SubmitSuccessActivity::class.java)
        intent.putExtra("apply_sn",code)
        startActivity(intent)
    }
}